//
//  SearchView.m
//  ChineseCollegeStudentsNetwork
//
//  Created by 刘喆 on 2018/5/13.
//  Copyright © 2018年 刘喆. All rights reserved.
//

#import "SearchView.h"

@interface SearchView ()<UITextFieldDelegate>
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIButton *searchButton;
@property (nonatomic, strong) TextChangeBlock textBlock;
@end

@implementation SearchView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        [self setupLayout];
    }
    return self;
    
}
- (void)setupUI {
    self.searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.searchButton setTitle:@"搜索" forState:UIControlStateNormal];
    [self.searchButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [[self.searchButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        assert(self.textBlock);
        if (self.textField.text.length > 0) {
            self.textField.text = @"";
            [self.textField endEditing:YES];
            self.textBlock(self.textField.text);
        }
     }];
    
    self.textField = [[UITextField alloc] init];
    self.textField.placeholder = @"输入关键词";
    self.textField.delegate = self;
    self.textField.borderStyle = UITextBorderStyleRoundedRect;
    self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    
}
- (void)setupLayout {
    [self addSubview:self.searchButton];
    [self.searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.size.mas_equalTo(CGSizeMake(44, 30));
        make.bottom.mas_equalTo(-10);
    }];
    [self addSubview:self.textField];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.bottom.mas_equalTo(-10);
        make.right.mas_equalTo(self.searchButton.mas_left).offset = -10;
        make.height.mas_equalTo(30);
    }];
}


- (void)didSearchTextString:(TextChangeBlock)changeBlock {
    
    self.textBlock = changeBlock;
}




@end
